package com.useeinfo.asg.open.admin;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.FrontUserBiz;
import com.useeinfo.asg.modules.logging.biz.FrontUserBiz;
import com.useeinfo.asg.modules.logging.entity.FrontUser;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 */
@Controller
@RequestMapping("/admin/frontUser")
public class FrontUserAdminManageAction {

    @Autowired
    private FrontUserBiz frontUserBiz;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, ModelMap mv) {

        return "/admin/frontUser/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(@RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "name", required = false) String sidxParam) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);

        return frontUserBiz.findJSONList(queryParam);
    }

    @RequestMapping("/addOredit")
    @ResponseBody
    public JSONObject add(HttpServletRequest request,String id,String frontUserName,String password) {
        JSONObject jsonObject = CommUtil.jsonResult(false);
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("userName",CommUtil.null2String(frontUserName));
        List<FrontUser>frontUserList = frontUserBiz.findList(queryParam);
        FrontUser frontUserOld = null;
        if (frontUserList.size()>0)
            frontUserOld=frontUserList.get(0);
        FrontUser frontUser = frontUserBiz.findModel(CommUtil.null2Long(id));
        if(null==frontUser){
            if(null==frontUserOld) {
                frontUser = new FrontUser();
                frontUser.setCreateTime(new Date());
            }else {
                jsonObject.put("msg","用户名已存在");
                return jsonObject;
            }
        }else {
             if(null!=frontUserOld&&!frontUser.getId().equals(frontUserOld.getId())){
                jsonObject.put("msg","用户名已存在");
                return jsonObject;
            }
            frontUser.setUpdateTime(new Date());
        }
        frontUser.setPassword(CommUtil.null2String(password));
        frontUser.setUserName(CommUtil.null2String(frontUserName));
        frontUserBiz.addOrUpdate(frontUser);
        jsonObject = CommUtil.jsonResult(true);
       return jsonObject;
    }



    @RequestMapping(value = "/editPage", produces = "text/html;charset=UTF-8")
    public String edit(HttpServletRequest request, ModelMap mv) {


        String id = request.getParameter("id");
        FrontUser frontUser = frontUserBiz.findModel(CommUtil.null2Long(id));
        mv.put("frontUser", frontUser);
        return "/admin/frontUser/edit";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public int delete(HttpServletRequest request, ModelMap mv) {

        String id = request.getParameter("id");
        FrontUser frontUser = frontUserBiz.findModel(CommUtil.null2Long(id));
        if(null!=frontUser)
            frontUser.setDeleteStatus(true);
        return frontUserBiz.update(frontUser);
    }
}
